package reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reggie.common.CustomException;
import reggie.entity.Category;
import reggie.entity.Dish;
import reggie.entity.Setmeal;
import reggie.mapper.CategoryMapper;
import reggie.service.CategoryService;
import reggie.service.DishService;
import reggie.service.SetmealService;

import java.util.Set;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    @Override
    public void remove(Long id) {
        //创建关于Dish的条件构造器
        LambdaQueryWrapper<Dish> DishqueryWrapper = new LambdaQueryWrapper<>();
        DishqueryWrapper.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(DishqueryWrapper);
        //判断count1是否存在
        if (count1 > 0) {
            //已经关联了相关菜品，抛出异常
            throw new CustomException("已经关联了菜品，无法删除");
        }
        //创建关于Setmeal的条件构造器
        LambdaQueryWrapper<Setmeal> SetmealqueryWrapper = new LambdaQueryWrapper<>();
        SetmealqueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(SetmealqueryWrapper);
        //判断count2是否存在
        if (count1 > 0) {
            //已经关联了相关菜品，抛出异常
            throw new CustomException("已经关联了套餐，无法删除");
        }
        super.removeById(id);
    }
}
